<%-- 
    Document   : controllerImovel
    Created on : 26/06/2012, 09:51:47
    Author     : Gledson
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="br.com.cadastros.bean.Imovel, br.com.ZeroHora.DAO.imovelDAO" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        try{
                String actionImovel = request.getParameter("actionImovel");

        if(actionImovel.equalsIgnoreCase("cadastrar")){
            Imovel i = new Imovel();
            i.setLogradouro(request.getParameter("logradouro"));
            i.setNumero(request.getParameter("numero"));
            i.setComplemento(request.getParameter("complemento"));
            i.setBairro(request.getParameter("bairro"));
            i.setMunicipio(request.getParameter("municipio"));
            i.setReferencia(request.getParameter("referencia"));
            i.setValorAluguel(Float.parseFloat(request.getParameter("valorAluguel")));
            i.setValorVenda(Float.parseFloat(request.getParameter("valorVenda")));
            i.setValorIPTU(Float.parseFloat(request.getParameter("valorIPTU")));
            i.setDescricao(request.getParameter("descricao"));

            imovelDAO iDAO= new imovelDAO();
                       
            try{
                iDAO.cadastrar(i);
                request.setAttribute("msg", "Imóvel Cadastrado com sucesso");
                request.getRequestDispatcher("cadastroImovel.jsp").forward(request, response);
            }catch(Exception erro){
                out.print("O erro foi: " + erro.getMessage());                                           
        }
              }
               else if(actionImovel.equalsIgnoreCase("excluir")){
                   try{
                       Integer idImovel = Integer.parseInt(request.getParameter("idImovel"));
                       imovelDAO iDAO = new imovelDAO();
                       Imovel i = iDAO.busca(idImovel);
                       if(i.getIdImovel()>0){
                           iDAO.delete(i);                       
                           request.setAttribute("msg", "Imóvel Deletado com sucesso");
                           request.getRequestDispatcher("listarImovel.jsp").forward(request, response);
            }          
                   }catch(Exception e){
                       out.print("Eroo: " + e.getMessage());
                   }                                                        
                                 }
               else if (actionImovel.equalsIgnoreCase("update")) {
                    Imovel i = new Imovel();
                        i.setIdImovel(new Integer(request.getParameter("idImovel")));
                        i.setLogradouro(request.getParameter("logradouro"));
                        i.setNumero(request.getParameter("numero"));
                        i.setComplemento(request.getParameter("complemento"));
                        i.setBairro(request.getParameter("bairro"));
                        i.setMunicipio(request.getParameter("municipio"));
                        i.setReferencia(request.getParameter("referencia"));
                        i.setValorAluguel(Float.parseFloat(request.getParameter("valorAluguel")));
                        i.setValorVenda(Float.parseFloat(request.getParameter("valorVenda")));
                        i.setValorIPTU(Float.parseFloat(request.getParameter("valorIPTU")));
                        i.setDescricao(request.getParameter("descricao"));

                        imovelDAO iDAO = new imovelDAO();
            
            try{
                iDAO.update(i);
                request.setAttribute("msg", "Imovel Atualizado com sucesso");
                request.getRequestDispatcher("listarImovel.jsp").forward(request, response);
            }catch(Exception erro){
                out.print("Erro:" + erro.getMessage());
                               }
                                               }
               }catch(NullPointerException nexp){
                   out.print("Erro na operação" + nexp);
               }
                              
    %>
    </body>
</html>
